package com.laoniu.biz.base;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.laoniu.dto.PageResponse;
import com.laoniu.dto.RestResult;
import com.laoniu.dto.request.BaseSupplierRequestDto;
import com.laoniu.dto.response.BaseSupplierDto;
import com.laoniu.entity.BaseSupplier;
import com.laoniu.entity.SecurityUser;
import com.laoniu.service.base.BaseSupplierService;
import com.laoniu.utils.SpringSecurityUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class BaseSupplierBiz {

    @Autowired
    private BaseSupplierService supplierService;

    /**
     * 分页查询
     * @param requestDto
     * @return
     */
    public PageResponse<BaseSupplierDto> pageList(BaseSupplierRequestDto requestDto) {
        Page<BaseSupplier> page = supplierService.listPage(requestDto);
        PageResponse<BaseSupplierDto> result = new PageResponse<BaseSupplierDto>();
        result.setPageNo(requestDto.getPageNo());
        result.setPageSize(requestDto.getPageSize());
        result.setTotal(page.getTotal());
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            List<BaseSupplierDto> resultList = Lists.newArrayList();
            for (BaseSupplier record : page.getRecords()) {
                BaseSupplierDto convert = Convert.convert(BaseSupplierDto.class, record);
                resultList.add(convert);
            }
            result.setData(resultList);
        }
        return result;
    }

    /**
     * 添加供应商
     * @param dto
     */
    public void addSupplier(BaseSupplierDto dto) {
        BaseSupplier baseSupplier = Convert.convert(BaseSupplier.class, dto);
        SecurityUser user = SpringSecurityUtils.getUser();
        baseSupplier.setCreateTime(LocalDateTime.now());
        baseSupplier.setUpdateTime(LocalDateTime.now());
        baseSupplier.setOpId(user.getId());
        baseSupplier.setOpName(user.getName());
        supplierService.save(baseSupplier);
    }

    /**
     * 更新供应商
     * @param dto
     */
    public void updateSupplier(BaseSupplierDto dto) {
        if (Objects.isNull(dto.getId())){
            return;
        }
        BaseSupplier baseSupplier = supplierService.getById(dto.getId());
        if (Objects.isNull(baseSupplier)){
            return;
        }
        SecurityUser user = SpringSecurityUtils.getUser();
        baseSupplier.setName(dto.getName());
        baseSupplier.setAddress(dto.getAddress());
        baseSupplier.setPhone(dto.getPhone());
        baseSupplier.setEmail(dto.getEmail());
        baseSupplier.setPostCode(dto.getPostCode());
        baseSupplier.setContactNumber(dto.getContactNumber());
        baseSupplier.setContactPerson(dto.getContactPerson());
        baseSupplier.setBank(dto.getBank());
        baseSupplier.setBankNum(dto.getBankNum());
        baseSupplier.setStatus(dto.getStatus());
        baseSupplier.setUpdateTime(LocalDateTime.now());
        baseSupplier.setOpId(user.getId());
        baseSupplier.setOpName(user.getName());
        supplierService.updateById(baseSupplier);
    }

    /**
     * 删除供应商
     * @param id
     * @return
     */
    public RestResult delete(Long id) {
        if (Objects.isNull(id)){
            return RestResult.fail("id不能为空");
        }
        BaseSupplier supplier = supplierService.getById(id);
        if (Objects.isNull(supplier)){
            return RestResult.fail("id无效");
        }
        SecurityUser user = SpringSecurityUtils.getUser();
        if (!user.getId().equals(supplier.getOpId())) {
            return RestResult.fail("您不能删除该供应商");
        }
        supplierService.removeById(id);
        return RestResult.success();
    }

    /**
     * 查询所有的供应商
     * @return
     */
    public List<BaseSupplierDto> allSupplier() {
        List<BaseSupplier> list = supplierService.list();
        List<BaseSupplierDto> result = Lists.newArrayList();
        if (CollectionUtils.isEmpty(list)){
            return result;
        }
        for (BaseSupplier supplier : list) {
            BaseSupplierDto dto = Convert.convert(BaseSupplierDto.class, supplier);
            result.add(dto);
        }
        return result;
    }
}
